<?php
if (!defined('XOOPS_ROOT_PATH')) exit();

require_once XOOPS_MODULE_PATH. '/scuttle/class/AbstractViewAction.class.php';
require_once XOOPS_MODULE_PATH. '/scuttle/include/functions.php';
require_once XOOPS_MODULE_PATH. '/scuttle/include/TagService.php';
require_once XOOPS_ROOT_PATH . "/core/XCube_PageNavigator.class.php";

class Scuttle_TagAction extends Scuttle_AbstractViewAction
{
	var $wordObject= array();
	var $getTagWordArray = array();
	var $tagWordArray = array();
	var $bookmarksArray =array();
	var $perPage = 20;    // page count
	var $moduleHeader = null;
	var $mPagenavi = null;
	var $searchTagsArray = null;

	function _getTag()
	{
		return xoops_getrequest('tags');
	}

	function &_getHandler()
	{
		$handler =& xoops_getmodulehandler('tags');
		return $handler;
	}

	function _getStart()
	{
		return xoops_getrequest('start');
	}

	function _getBaseUrl()
	{
		return 'index.php?action=Tag';
	}

	function &_getPageNavi()
	{
		$navi =& new XCube_PageNavigator($this->_getBaseUrl(), XCUBE_PAGENAVI_START);
		$navi->setStart($this->_getStart());
		$navi->setPerpage($this->perPage);
		$navi->setTotalItems($this->totalCount);
		// add pagenavi parameter
		$navi->addExtra('tags', $this->_getTag());
		return $navi;
	}

	function prepare(&$controller, &$xoopsUser, $moduleConfig)
	{
		parent::prepare(&$controller, $xoopsUser, $moduleConfig);
		$this->mConfig = $moduleConfig;

		if ($controller->mRoot->mContext->mUser->isInRole('Module.scuttle.Admin')) {
			$this->isAdmin = true;
		}

	}

	function getDefaultView(&$controller, &$xoopsUser)
	{
		$root =& XCube_Root::getSingleton();
		$db =& $root->mController->mDB;

		$handler =& $this->_getHandler();
		$getTag = $this->_getTag() ;
		$startEntry = $this->_getStart() ;

		// tag text change to array
		$searchTags = mb_convert_encoding(htmlspecialchars(urldecode($getTag), ENT_QUOTES) ,'UTF-8','auto');

		// $searchTags = urldecode($getTag);

		// OPTION --- need check your site encording ---
		// $myencoding = mb_detect_encoding($searchTags);
		// $searchTags = mb_convert_encoding(htmlspecialchars(urldecode($getTag), ENT_QUOTES) ,'UTF-8',$myencoding);

		$this->searchTagsArray = explode('--', $searchTags);

		if (count($this->searchTagsArray)> 1) {
			// tagg + tag + tag to arreay is serch AND
			$this->bookmarksArray = TagService::getTagBookmarksAnd($this->searchTagsArray, $startEntry, $this->perPage);
			$this->totalCount     = TagService::getTagBookmarksAndCount($this->searchTagsArray);
		} else {
			// tag is single word
			$this->bookmarksArray = TagService::getTagBookmarks($this->searchTagsArray, $startEntry,$this->perPage);
			$this->totalCount     = TagService::getTagBookmarksCount($this->searchTagsArray);
		}

		if ($this->isAdmin) {
			// make jquery-in-place-editor javascript
			$inplaceJavaScript = null;
			foreach($this->bookmarksArray as $module) {
				$inplaceJavaScript.= "$(document).ready(function(){
					$(\".editme". $module['bId']. "\").editInPlace({
						url: \"inplace.php?id=". $module['bId']. "\",
						params: \"ajax=yes\",
						saving_text:\"". _MD_SC_INPLACE_SAVING. "\",
						save_button:'<input type=\"submit\" class=\"inplace_save\" value=\"". _MD_SC_INPLACE_SAVE. "\"/>',
						cancel_button:'<input type=\"submit\" class=\"inplace_cancel\" value=\"". _MD_SC_INPLACE_CANSEL. "\"/>',
						text_size:\"50\",
					});
				});" ;
			}
		}


		// Page navi
		if ($this->totalCount > $this->perPage ) {
			$this->mPagenavi =$this->_getPageNavi();
			$this->mPagenavi->fetch();
		}

		// Page Component
		$this->breadcrumbs[] = array('name' => $root->mContext->mModule->mXoopsModule->getVar('name') , 'url' => XOOPS_MODULE_URL.'/scuttle/' ) ;
		$this->breadcrumbs[] = array('name' => _MD_SC_TAGCLOUD , 'url' => XOOPS_MODULE_URL.'/scuttle/index.php?action=TagCloud' ) ;
		$this->breadcrumbs[] = array('name' => $searchTags )  ;
		$this->pageTitle = $searchTags .'&nbsp;-&nbsp;'.  $root->mContext->mModule->mXoopsModule->getVar('name')  ;

		$this->moduleHeader.= '<script type="text/javascript" src="' . XOOPS_URL . '/common/js/jQuery/jquery.js"></script>'."\n";
		$this->moduleHeader.= '<script type="text/javascript" src="' . XOOPS_URL . '/common/js/jQuery/prettyPhoto/jquery.prettyPhoto.js"></script>'."\n";

		if ($this->isAdmin) {
			$this->moduleHeader.= '<script type="text/javascript" src="' . XOOPS_MODULE_URL .'/scuttle/js/jsDelete.php"></script>'."\n";
			// http://code.google.com/p/jquery-in-place-editor/
			$this->moduleHeader.= '<script type="text/javascript" src="' . XOOPS_MODULE_URL . '/scuttle/js/jquery.inplace.pack.js"></script>'."\n";
			$this->moduleHeader.= '<script type="text/javascript">'. $inplaceJavaScript. '</script>'."\n";
		}
		$this->moduleHeader.= '<link rel="stylesheet" type="text/css" href="' . XOOPS_MODULE_URL. '/scuttle/module.css" />'."\n";
		$this->moduleHeader.= '<link rel="stylesheet" type="text/css" href="' .  XOOPS_URL . '/common/js/jQuery/prettyPhoto/css/prettyPhoto.css" />'."\n";

		return SC_FRAME_VIEW_INDEX;
	}

	function executeViewIndex(&$controller, &$xoopsUser, &$render)
	{
		$render->setTemplateName('scuttle_tag.html');
		$render->setAttribute('get_tag'     , $this->_getTag());
		$render->setAttribute('bookmarks_array'  , $this->bookmarksArray);
		$render->setAttribute('total_count' , $this->totalCount);
		$render->setAttribute('pageNavi'   , $this->mPagenavi);
		$render->setAttribute('search_tags' , $this->searchTagsArray);

		$render->setAttribute('rangeOptions', array("0" => _MD_SC_BOOKMARKS_RANGE_0, "2" => _MD_SC_BOOKMARKS_RANGE_1, "1" => _MD_SC_BOOKMARKS_RANGE_2));
		$render->setAttribute('pretty_photo'  , true);
		$render->setAttribute('xoops_breadcrumbs' ,$this->breadcrumbs) ;
		$render->setAttribute('xoops_pagetitle'  , $this->pageTitle );
		$render->setAttribute('xoops_module_header',$this->moduleHeader);
	}
}
?>

